*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	This program creates a file with NYC school characteristics
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
args bw
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

**	enrollment characteristics

*	load enrollment file

	//this is a student by year file
	use "${cleandata}demo_all_years_ms", clear

	//restrict to students in the grade range specified
	destring grade, replace force
	keep if grade==6
	keep if inrange(year, 2017, 2019)

	//destring school codes and student ID
	replace sch = subinstr(sch,"B","1",.)
	replace sch = subinstr(sch,"K","2",.)
	replace sch = subinstr(sch,"M","3",.)
	replace sch = subinstr(sch,"Q","4",.)
	replace sch = subinstr(sch,"R","5",.)
	replace sch = subinstr(sch,"X","6",.)
	destring sch, replace

	//collapse
	egen tag = tag(sch year)
	keep if tag
	keep sch year

** identify screened schools

	preserve
		do "${code_build}b_screened_ms" `bw'
	restore

	preserve
		clear
		set obs 1
		gen year = .
		forval y =2016/2018{
			append using "${builddata}screened_dbns_ms_`y'", gen(new)
			replace year = `y' if new
			drop new
		}
		drop in 1
		rename app_dbn_last_4 sch
		rename scr4 screened
		keep sch screened year
		replace sch = subinstr(sch,"B","1",.)
		replace sch = subinstr(sch,"K","2",.)
		replace sch = subinstr(sch,"M","3",.)
		replace sch = subinstr(sch,"Q","4",.)
		replace sch = subinstr(sch,"R","5",.)
		replace sch = subinstr(sch,"X","6",.)
		destring sch, replace
		drop if mi(sch)
		****
		replace year = year + 1
		***
		tempfile screened_schs
		save "`screened_schs'"
	restore
	merge m:1 sch year using "`screened_schs'", keep(1 3) nogen
	replace screened = 0 if mi(screened)

***	save

	save "${builddata}${city}_schoolcovs", replace
